package cn.kn.sys.service.impl;

import cn.kn.common.model.R;
import cn.kn.sys.dao.RoleRecouresDao;
import cn.kn.sys.mapper.RoleResourcesMapper;
import cn.kn.sys.model.RoleResources;
import cn.kn.sys.model.RoleResourcesExample;
import cn.kn.sys.service.RoleResourcesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author WangJiang
 * @create 2020-12-11 13:53
 */
@Service
@RequiredArgsConstructor
public class RoleResourcesServiceImpl extends BaseService implements RoleResourcesService {

    private final RoleResourcesMapper resourcesMapper;
    private final RoleRecouresDao roleRecouresDao;


    @Transactional
    @Override
    public R save(Long roleId, RoleResources[] resources) {
        RoleResourcesExample example=new RoleResourcesExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        resourcesMapper.deleteByExample(example);
        List<RoleResources> roleResourcesList= Arrays.asList(resources);
        if(!CollectionUtils.isEmpty(roleResourcesList)){
            roleRecouresDao.save(roleResourcesList);
        };
        return R.ok();

    }
}
